package com.hilaver.dzmis.service.impl;

import java.util.List;

import org.eclipse.jdt.internal.compiler.ast.SuperReference;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.codetable.CtAccessoryType;
import com.hilaver.dzmis.codetable.CtApplicationType;
import com.hilaver.dzmis.product.ProductAccessoryOthers;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class ProductAccessoryOthersServiceImpl extends AbstractBaseService {
	
	public final static String COLUMN_PI_ID = "productIdentification.id";
	
	public final static String COLUMN_ACCESSORY_OTHERS_ID = "aoId";
	
	public final static String COLUMN_ACCESSORY_OTHERS_REFERENCE = "aoReference";
	
	public final static String COLUMN_ACCESSORY_OTHERS_CT_BRAND = "ctBrand";
	
	public final static String COLUMN_ACCESSORY_OTHERS_CT_CATEGORY = "ctAccessoryType";
	
	public final static String COLUMN_ACCESSORY_OTHERS_CT_MATERIAL = "ctMaterial";
	
	private CtServiceImpl ctService;
	private BiAccessoryOthersServiceImpl biAccessoryOthersServiceImpl;
	
	public ProductAccessoryOthersServiceImpl(){
		this.ctService = new CtServiceImpl();
		this.biAccessoryOthersServiceImpl = new BiAccessoryOthersServiceImpl();
	}
	
	public String edit(ProductAccessoryOthers pao) throws Exception{
		String operation = null;
		if(pao.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(pao);
		super.editSysMbox(pao, operation);
		return "{success: true}";
	}
	
	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(ProductAccessoryOthers.class.getName(), id);
		return super.delete(ProductAccessoryOthers.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		ProductAccessoryOthers obj = (ProductAccessoryOthers)this.dao.get(ProductAccessoryOthers.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		xmlSB.append(this.getOtherInfoStr(obj));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAll(Integer piId) throws Exception{
		String [] filters = new String[1];
		filters[0] = COLUMN_PI_ID + "=" + piId;
		List<ProductAccessoryOthers> objList = this.dao.getAll(ProductAccessoryOthers.class.getName(), COLUMN_ID, "asc", filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (ProductAccessoryOthers obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(this.getOtherInfoStr(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductAccessoryOthers.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllAccessoryOthers(Integer piId) throws Exception{
		biAccessoryOthersServiceImpl.setLocale(this.locale);
		String [] filters = new String[1];
		filters[0] = COLUMN_PI_ID + "=" + piId;
		List<ProductAccessoryOthers> objList = this.dao.getAll(ProductAccessoryOthers.class.getName(), COLUMN_ID, "asc", filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (ProductAccessoryOthers obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append(biAccessoryOthersServiceImpl.toXMLFragment(obj.getBiAccessoryOthers()));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductAccessoryOthers.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	private String getOtherInfoStr(ProductAccessoryOthers accessoryOthers) throws Exception{
		StringBuffer sb = new StringBuffer();
		
		sb.append("<" + COLUMN_ACCESSORY_OTHERS_ID + ">");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(accessoryOthers.getBiAccessoryOthers().getId());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</" + COLUMN_ACCESSORY_OTHERS_ID + ">");
		
		sb.append("<ctApplicationType>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(this.ctService.getDescription(CtApplicationType.class.getName(), accessoryOthers.getBiAccessoryOthers().getCtApplicationType(), locale));
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</ctApplicationType>");
		
		sb.append("<applicatoinReference>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(accessoryOthers.getBiAccessoryOthers().getApplicationReference()==null?"":accessoryOthers.getBiAccessoryOthers().getApplicationReference());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</applicatoinReference>");
		
		sb.append("<ctAccessoryType>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(this.ctService.getDescription(CtAccessoryType.class.getName(), accessoryOthers.getBiAccessoryOthers().getCtAccessoryType(), locale));
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</ctAccessoryType>");
		
		sb.append("<accessoryReference>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(accessoryOthers.getBiAccessoryOthers().getAccessoryReference()==null?"":accessoryOthers.getBiAccessoryOthers().getAccessoryReference());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</accessoryReference>");
		
		sb.append("<customerReference>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(accessoryOthers.getBiAccessoryOthers().getCustomerReference()==null?"":accessoryOthers.getBiAccessoryOthers().getCustomerReference());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</customerReference>");
		
		String supplier = ""; //后加需求，根据 辅料类型确定是 factory,custoer还是DZ。hardcode
		String factoryNameCN = null; //只是一个标记，并不是真正的factoryName,只是为了页面显示方便。
		String factoryNameEN = null;
		if("01".equals(accessoryOthers.getBiAccessoryOthers().getCtSupplyType())){
			supplier = accessoryOthers.getBiAccessoryOthers().getBiFranceCustomer().getName();
			factoryNameCN = accessoryOthers.getBiAccessoryOthers().getBiFranceCustomer().getName();
			factoryNameEN = accessoryOthers.getBiAccessoryOthers().getBiFranceCustomer().getDepartment();
		}else if("02".equals(accessoryOthers.getBiAccessoryOthers().getCtSupplyType())){
			if(Constants.LOCALE_TYPE_EN.equals(locale)){
				supplier = accessoryOthers.getBiAccessoryOthers().getBiFactory().getNameEN();
			}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
				supplier = accessoryOthers.getBiAccessoryOthers().getBiFactory().getNameCN();
			}else{
				supplier = accessoryOthers.getBiAccessoryOthers().getBiFactory().getNameEN();
			}
			
		}else{
			supplier = "DZ";
			factoryNameCN = "DZ";
			factoryNameEN = "DZ";
		}
		sb.append("<supplier>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(supplier);
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</supplier>");
	
		if(factoryNameCN != null){
			sb.append("<factoryNameCN>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(factoryNameCN);
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</factoryNameCN>");
			
			sb.append("<factoryNameEN>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(factoryNameEN);
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</factoryNameEN>");
		}
		
		
		
		this.biAccessoryOthersServiceImpl.setRealFileDir(this.realFileDir);
		this.biAccessoryOthersServiceImpl.setLocale(this.locale);
		sb.append(this.biAccessoryOthersServiceImpl.toXMLFragment(accessoryOthers.getBiAccessoryOthers()));
		
		sb.append(SimpleObj2XML.toXMLFragment(accessoryOthers.getBiAccessoryOthers()));
		
		return sb.toString();
	}

	public static void main(String[] args) throws Exception{
		ProductAccessoryOthersServiceImpl os = new ProductAccessoryOthersServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}
}



